package top.lidee.module.clientApi.twice.applet;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.lidee.common.dto.UserInfo;
import top.lidee.common.service.MerchantService;
import top.lidee.common.service.StoreService;
import top.lidee.common.util.TokenUtil;
import top.lidee.framework.enums.CommonExceptionEnum;
import top.lidee.framework.result.ResponseResult;
import top.lidee.repository.mapper.MtUserMapper;
import top.lidee.repository.model.twice.vo.applet.AddStoreLicenseInformationVo;
import top.lidee.repository.model.twice.vo.applet.MtMerchantVo;
import top.lidee.repository.model.twice.vo.applet.UpdateByStoreVo;
import top.lidee.repository.model.twice.vo.management.ASLISubjectDataACardVo;
import top.lidee.repository.model.twice.vo.management.BusinessInformationCheckVo;
import top.lidee.repository.model.twice.vo.management.SettlementBankCheckVo;
import top.lidee.utils.StringUtil;

import javax.servlet.http.HttpServletRequest;

import static top.lidee.framework.result.GlobalCheck.checkMerchantId;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/9/24 13:11
 */
@Api(tags = "小程序-店铺相关接口")
@RestController
@AllArgsConstructor
@RequestMapping(value = "/clientApi/applet/store")
public class MtStoreController {

    private final StoreService storeService;

    private final MtUserMapper mtUserMapper;



    /**
     * 1.描述：根据选中的分类并且需要特殊资质时，查询出需要哪些特殊资质图片，至少传一张
     * 2.入参：
     * 1）选中的主体类型值0个体户，1企业，2小微/个人（主要用于微信主体类型（哪个主体类型店铺必须传,至少传一张）。支付宝是特殊行业就要传这个支付宝进件时有校验）
     * 2）选中的二级类目id
     */
    @RequestMapping(value = "/getSpecialMapByTypeACateId", method = RequestMethod.GET)
    @ApiOperation(value = "根据选中的分类并且需要特殊资质时，查询出需要哪些特殊资质图片")
    public ResponseResult getSpecialMapByTypeACateId(HttpServletRequest request
            , @RequestParam("principalType") String principalType
            , @RequestParam("cateId") Integer cateId) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        Integer merchantId = mtUserMapper.selectById(userInfo.getId()).getMerchantId();
        checkMerchantId(merchantId);
        ResponseResult responseResult = storeService.getSpecialMapByTypeACateId( principalType, cateId);

        return responseResult;
    }

    /**
     * 中国各大银行集合
     */
    @RequestMapping(value = "/getRedliningList", method = RequestMethod.GET)
    @ApiOperation(value = "中国各大银行集合")
    public ResponseResult getRedliningList(HttpServletRequest request) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = storeService.getRedliningList();

        return responseResult;
    }

    /**
     * 证照信息第一步校验
     * 主题信息、营业执照&身份证
     */
    @RequestMapping(value = "/asliSubjectDataACard", method = RequestMethod.POST)
    @ApiOperation(value = "主题信息、营业执照&身份证第一步校验")
    public ResponseResult asliSubjectDataACard(HttpServletRequest request, @RequestBody @Validated ASLISubjectDataACardVo vo) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        Integer merchantId = mtUserMapper.selectById(userInfo.getId()).getMerchantId();
        //判断当前用户是否是商户
        checkMerchantId(merchantId);

        ResponseResult responseResult = storeService.asliSubjectDataACard(vo);

        return responseResult;
    }

    /**
     * 证照信息第二步校验
     * 经营资料
     */
    @RequestMapping(value = "/businessInformationCheck", method = RequestMethod.POST)
    @ApiOperation(value = "经营资料")
    public ResponseResult businessInformationCheck(HttpServletRequest request, @RequestBody @Validated BusinessInformationCheckVo vo) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = storeService.businessInformationCheck(vo);

        return responseResult;
    }


    /**
     * 证照信息第三步校验
     * 结算银行账户
     */
    @RequestMapping(value = "/settlementBankCheck", method = RequestMethod.POST)
    @ApiOperation(value = "结算银行账户")
    public ResponseResult settlementBankCheck(HttpServletRequest request, @RequestBody @Validated SettlementBankCheckVo vo) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        ResponseResult responseResult = storeService.settlementBankCheck(vo);

        return responseResult;
    }


    /**
     * 新增店铺基本信息和证照信息
     */
    @RequestMapping(value = "/addStoreLicenseInformation", method = RequestMethod.POST)
    @ApiOperation(value = "新增店铺基本信息和证照信息")
    public ResponseResult uploadLicense(HttpServletRequest request,
                                        @RequestBody @Validated AddStoreLicenseInformationVo vo) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        Integer merchantId = mtUserMapper.selectById(userInfo.getId()).getMerchantId();
        //判断当前用户是否是商户
        checkMerchantId(merchantId);
        vo.setMerchantId(merchantId);
        ResponseResult responseResult = storeService.uploadLicense(vo);

        return responseResult;
    }


    /**
     * 根据id查询当前店铺可以修改的信息
     */
    @RequestMapping(value = "/getStoreLicenseInformationById/{storeId}/{type}", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查询当前店铺可以修改的信息")
    public ResponseResult getStoreLicenseInformationById(HttpServletRequest request,
                                                         @PathVariable("storeId") Integer storeId,
                                                         @PathVariable("type") String type) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        Integer merchantId = mtUserMapper.selectById(userInfo.getId()).getMerchantId();
        checkMerchantId(merchantId);
        ResponseResult responseResult = storeService.getStoreLicenseInformationById(storeId, merchantId,type);

        return responseResult;
    }


    /**
     * 根据id修改当前店铺 如果支付宝或微信有一个审核状态为进件中状态或进件成功则不能修改，如果为人工审核成功状态则重新纳入审核状态
     */
    @RequestMapping(value = "/updateByStore", method = RequestMethod.PUT)
    @ApiOperation(value = "根据id修改当前店铺 如果审核状态为进件中状态则不能修改，如果为人工审核成功状态则查询纳入审核")
    public ResponseResult updateByStore(HttpServletRequest request,
                                        @RequestBody @Validated UpdateByStoreVo vo) {
        String token = request.getHeader("Access-Token");

        if (StringUtil.isEmpty(token)) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }

        UserInfo userInfo = TokenUtil.getUserInfoByToken(token);
        if (userInfo == null) {
            return ResponseResult.errorResult(CommonExceptionEnum.NOAUTH);
        }
        Integer merchantId = mtUserMapper.selectById(userInfo.getId()).getMerchantId();
        checkMerchantId(merchantId);
        vo.setMerchantId(merchantId);
        ResponseResult responseResult = storeService.updateByStore(vo);

        return responseResult;
    }


}
